<?php
/**
 * Created by PhpStorm.
 * User: t
 * Date: 2024/6/12
 * Time: 19:33

 * @copyright: ©2024 永州微聚商 网络科技有限公司
 * @link: https://www.weiJuyunke.com

 */


namespace app\admin\controller;


use app\common\model\CircleComment;

use app\common\model\CircleCommentLike;
use app\common\model\MessageTemplate;
use app\common\model\NoticeMessage;
use app\Request;
use think\Exception;

class Circle
{
    public function  circleList (Request $request){
        $param = $request->param();
        $limit = 10;
        $start = 0;
        if (isset($param['limit']) && !empty($param['limit'])) {
            $limit = $param['limit'];
        }
        if (isset($param['page']) && !empty($param['page'])) {
            $start = ($param['page'] - 1) * $limit;
        }
        $where  = [];
        if (isset($param['title']) && !empty($param['title'])) {
            $where[] = ['c.title','like','%'.$param['title'].'%'];
        }
        if (isset($param['name']) && !empty($param['name'])) {
            $where[] = ['u.nickname|u.phone','like','%'.$param['name'].'%'];
        }
        if (isset($param['start_time']) && !empty($param['start_time'])){
            $where[] = ['c.create_time','>=',strtotime($param['start_time'])];
        }
        if (isset($param['status']) && !empty($param['status'])){
            $where[] = ['c.status','>=',strtotime($param['status'])];
        }
        if (isset($param['end_time']) && !empty($param['end_time'])){
            $where[] = ['c.create_time','<=',strtotime($param['end_time'])];
        }
        $circleModel = new \app\common\model\Circle();
        $list = $circleModel->alias('c')
            ->leftJoin('user u','c.uid  = u.id')
            ->field('c.*,u.nickname,u.avatar,u.phone')
            ->order('c.create_time','desc')
            ->limit($start,$limit)
            ->select()->toArray();
        $count = $circleModel->alias('c')
            ->leftJoin('user u','c.uid  = u.id')
            ->limit($start,$limit)
           ->count();
        return success('成功',['list' => $list,'count' => $count]);
    }
    public function circleInfo(Request $request){
        $param = $request->param();
        $circleModel = new \app\common\model\Circle();
        $info = $circleModel->alias('c')
            ->where(['c.id' => $param['id']])
            ->leftJoin('user u','c.uid  = u.id')
            ->field('c.*,u.nickname,u.avatar')
           ->find();
        $userModel = new \app\common\model\User();
        if ($info['notice_user']){
            $info['notice_user_list'] =  $userModel->where('id','in',$info['notice_user'])->field('id,nickname,avatar')->select()->toArray();
        }else{
            $info['notice_user_list'] = [];
        }
        return  success('圈子详情',$info);

    }
    public function commentList(Request $request){
        $param = $request->param();
        $limit = 10;
        $start = 0;
        if (isset($param['limit']) && !empty($param['limit'])) {
            $limit = $param['limit'];
        }
        if (isset($param['page']) && !empty($param['page'])) {
            $start = ($param['page'] - 1) * $limit;
        }
        $where = [];
        if (isset($param['circle_id']) && !empty($param['circle_id'])) {
            $where[] = ['circle_id' ,'=' , $param['circle_id']];
        }
        if (isset($param['title']) && !empty($param['title'])) {
            $where[] = ['c.title' ,'like' , '%'.$param['title'].'%'];
        }
        if (isset($param['staus']) && !empty($param['staus'])) {
            $where[] = ['c.staus' ,'=' ,$param['staus']];
        }
        if (isset($param['circle_user']) && !empty($param['circle_user'])) {
            $where[] = ['cu.nickname|cu.phone' ,'like' , '%'.$param['circle_user'].'%'];
        }
        if (isset($param['comment_user']) && !empty($param['comment_user'])) {
            $where[] = ['ccu.nickname|ccu.phone' ,'=' , $param['comment_user']];
        }

        if (isset($param['reply_user']) && !empty($param['reply_user'])) {
            $where[] = ['ru.nickname|ru.phone' ,'=' , $param['reply_user']];
        }
        $circleCommentModel = new CircleComment();
        $circleCommentList  = $circleCommentModel
            ->alias('cc')
            ->leftJoin('circle c','cc.circle_id  = c.id')
            ->leftJoin('user cu','c.uid  = cu.id')
            ->leftJoin('user ru','cc.reply_uid  = ru.id')
            ->leftJoin('user ccu','cc.uid  = ccu.id')
            ->field('cc.*,cu.nickname as circle_user_nickname,cu.avatar as  circle_user_avatar ,cu.phone  as circle_user_phone,
            ccu.nickname as comment_user_nickname,ccu.avatar as  comment_user_avatar ,ccu.phone  as comment_user_phone,
              ru.nickname as reply_user_nickname,ru.avatar as  reply_user_avatar ,ru.phone  as reply_user_phone,
              c.title,c.img ,c.like_number as circle_like_number, c.comment_number as circle_comment_number')
            ->where($where)
            ->order('cc.create_time','desc')
            ->limit($start,$limit)->select()->toArray();
        $count  = $circleCommentModel
            ->alias('cc')
            ->leftJoin('circle c','cc.circle_id  = c.id')
            ->leftJoin('user cu','c.uid  = cu.id')
            ->leftJoin('user ru','cc.reply_uid  = ru.id')
            ->leftJoin('user ccu','cc.uid  = ccu.id')
            ->where($where)
            ->limit($start,$limit)->count();
        return success('圈子评论',['list' => $circleCommentList,'count' => $count]);

       /* if (isset($param['circle_id']) &&  )*/
    }
    public function updateCircleStatus(Request $request){
        $param = $request->param();
        $circleModel = new \app\common\model\Circle();
        $data['status']  =  $param['status'];
        if (isset($param['remark']) && !empty($param['remark'])){
            $data['remark'] = $param['remark'];
        }
        $circleInfo =  $circleModel->where(['id' => $param['id']])->find();
        $circleModel->where(['id' => $param['id']])->update($data);
        $messageModel = new MessageTemplate();
        $xcxMessage[]['data'] =  [
            'thing1' => ['value' => '圈子'],
            'thing2' => ['value' => $circleInfo['title']],
            // 'phrase3' => ['value' =>'通过'],
            'time5' => ['value' => date('Y-m-d H:i:s')],
        ];
        $userModel =  new \app\common\model\User();
        $userInfo  =  $userModel->where(['id' =>$circleInfo['uid'] ])->find();
        $gzhMessage[0]['data'] = [
            'thing10' => ['value' =>'圈子'],
            'thing28' => ['value' => $userInfo['nickname'] ],
            'thing9' => ['value' =>$circleInfo['title']],
        ];
        if ($param['status'] == 2){
            $xcxMessage[0]['data']['phrase3'] = '通过';
            $gzhMessage[0]['data']['thing2'] = '通过';
        }elseif ($param['status'] == 3){
            $xcxMessage[0]['data']['phrase3'] = '驳回';
            $gzhMessage[0]['data']['thing2'] = '驳回';
        }elseif ($param['status'] = 4){
            $xcxMessage[0]['data']['phrase3'] = '删除';
            $gzhMessage[0]['data']['thing2'] = '删除';
        }
        $xcxMessage[0]['uid'] = $circleInfo['uid'];
        $xcxMessage[0]['title'] ='release';
        $gzhMessage[0]['uid'] = $circleInfo['uid'];
        $gzhMessage[0]['title'] ='release';
        $messageModel->sendMessage(3,$xcxMessage);

        $messageModel->sendMessage(1,$gzhMessage);
        return  success('操作成功');
    }

    public function delCircle(Request $request){
        $param = $request->param();
        $circleModel = new \app\common\model\Circle();
        $circleModel->where(['id' =>$param['id'] ])->delete();
        $circleCommentModel =  new CircleComment();
        $circleCommentModel->where(['circle_id' =>$param['id'] ])->delete();
        $circleCommentLikeModel =  new CircleCommentLike();
        $circleCommentLikeModel->where(['circle_id' =>$param['id'] ])->delete();
        $noticeMessageModel =  new NoticeMessage();
        $noticeMessageModel->where(['circle_id' =>$param['id'] ])->where(['type','in',[1,2,3,4,5,6]])->delete();
        return success('操作成功');
    }



}